import java.io.IOException;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Listener;


public class login {

protected Shell shell;
protected static ClientLogic logic;

public login()
{
	logic = ClientLogic.getInstance();
}
/**
 * Launch the application.
 * @param args
 */
public static void main(String[] args) {
	try {
    	logic = ClientLogic.getInstance();
		login window = new login();
		window.open();
		window.shell.addDisposeListener(new DisposeListener(){
			public void widgetDisposed(DisposeEvent e) {
				try {
					logic.logout();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				System.exit(0);
			}
		});
	} catch (Exception e) {
		e.printStackTrace();
	}
}

/**
 * Open the window.
 */
public void open() {
	Display display = Display.getDefault();
	createContents();
	shell.open();
	shell.layout();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) {
			display.sleep();
		}
	}
}

        /**
         * Create contents of the window.
         */
        protected void createContents() {
                shell = new Shell();
                shell.setSize(500, 300);
                shell.setText("Login");
                
                Label lblServerIP = new Label(shell, SWT.NONE);
                lblServerIP.setBounds(10, 15, 91, 17);
                lblServerIP.setText("Server IP:");

                final Text ServerIP = new Text(shell, SWT.BORDER);
                ServerIP.setBounds(107, 5, 331, 27);
                /*
                Label lblServerPort = new Label(shell, SWT.NONE);
                lblServerPort.setBounds(10, 56, 91, 17);
                lblServerPort.setText("Server Port:");

                final Text ServerPort = new Text(shell, SWT.BORDER);
                ServerPort.setBounds(107, 46, 331, 27);
                */
                Label lblUserName = new Label(shell, SWT.NONE);
                lblUserName.setBounds(10, 106, 91, 17);
                lblUserName.setText("User Name:");

                final Text UserName = new Text(shell, SWT.BORDER);
                UserName.setBounds(107, 96, 331, 27);

                Label lblPassword = new Label(shell, SWT.NONE);
                lblPassword.setBounds(10, 156, 81, 17);
                lblPassword.setText("Password:");

                final Text Password = new Text(shell, SWT.BORDER | SWT.PASSWORD);
                Password.setBounds(107, 146, 331, 27);
                
                final Label resultLabel = new Label(shell, SWT.NONE);
                resultLabel.setBounds(107, 179, 331, 17);
                resultLabel.setVisible(false);
                
                
                Button btnLogIn = new Button(shell, SWT.NONE);
                btnLogIn.setBounds(84, 205, 91, 29);
                btnLogIn.setText("Log In");
                btnLogIn.addListener(SWT.Selection, new Listener() {
                    	public void handleEvent(org.eclipse.swt.widgets.Event event) {
								String errorMessage = "";
								if (UserName.getText().equals("")||Password.getText().equals("")) {
								resultLabel.setText("UserName and Password fields can't be empty.");
								    resultLabel.setVisible(true);
								} else {
				                          /* int test = 0;    
				                    	  try 
				                    	  { 
				                    		  test = Integer.parseInt(ServerPort.getText());
				                    		  if (test < 1024)
				                    		  {
				                    			  throw new Exception();
				                    		  }
				                    	  }
				                    	  catch (Exception e){
				                    		  resultLabel.setText("Port Number has to be integer greater than 1024.");
				                    	  }*/
					                    try {
					                    	errorMessage=logic.login(UserName.getText(), Password.getText(), ServerIP.getText());
					                    } catch (IOException e) {
					                    	// TODO Auto-generated catch block
					                    	e.printStackTrace();
									    }
					                    if (errorMessage.equals("")) {
					                    	//shell.close();
					                    } else {
										    resultLabel.setText(errorMessage);
										    resultLabel.setVisible(true);
										}
								}
                    	}
                });
                
                Button btnRegister = new Button(shell, SWT.NONE);
                btnRegister.setBounds(263, 205, 91, 29);
                btnRegister.setText("Register");
                btnRegister.addListener(SWT.Selection, new Listener() {
                        public void handleEvent(org.eclipse.swt.widgets.Event event) {
                           new register(Display.getCurrent().getActiveShell(), 0).open();
                        }
                });

        }
}
